
package cn.com.pusai.common.config;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.springframework.core.io.Resource;
/**
 * 读取 Properties基础操作类
 * @author 
 * @date 2012-09-13
 * @version 1.0
 * */
public class PusaiProperties extends Properties
{

	private static final long serialVersionUID = 1L;

	public PusaiProperties(){
	}

	public PusaiProperties(Resource location)throws IOException{
		load(location.getInputStream());
	}

	public static PusaiProperties getInstance(String location)
	{
		PusaiProperties myProperties;
		InputStream inputStream=null;
		myProperties = new PusaiProperties();
		if (location == null || location.length() <= 0)
		
		inputStream = null;
		if (location.toLowerCase().startsWith("classpath:"))
		{
			String path = location.substring("classpath:".length());
			if (!path.startsWith("/"))
				path = (new StringBuilder("/")).append(path).toString();
				inputStream = PusaiProperties.class.getResourceAsStream(path);
		} else
		{
			if (!location.startsWith("/"))
				location = (new StringBuilder("/")).append(location).toString();			
			    inputStream = PusaiProperties.class.getResourceAsStream(location);
		}
		try
		{
			myProperties.load(inputStream);
		}
		catch (IOException e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}finally{
		
		if (inputStream != null)
			try
			{
				inputStream.close();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		
		if (inputStream != null)
			try
			{
				inputStream.close();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		return myProperties;
	}

	public PusaiProperties(Resource locations[])throws IOException{
		Resource aresource[];
		int j = (aresource = locations).length;
		for (int i = 0; i < j; i++)
		{
			Resource location = aresource[i];
			load(location.getInputStream());
		}

	}

	public void load(Resource location)
		throws IOException
	{
		load(location.getInputStream());
	}

	public boolean getProperty(String name, boolean defaultValue)
	{
		String value = getProperty(name);
		return value != null ? Boolean.valueOf(value).booleanValue() : defaultValue;
	}

	public boolean getBooleanProperty(String name)
	{
		return getBooleanProperty(name, false);
	}

	public boolean getBooleanProperty(String name, boolean defaultValue)
	{
		return getProperty(name, defaultValue);
	}

	public int getProperty(String name, int defaultValue)
	{
		String value = getProperty(name);
		return value != null ? Integer.parseInt(value) : defaultValue;
	}

	public int getIntProperty(String name)
	{
		return getIntProperty(name, 0);
	}

	public int getIntProperty(String name, int defaultValue)
	{
		return getProperty(name, defaultValue);
	}

	public Integer getProperty(String name, Integer defaultValue)
	{
		String value = getProperty(name);
		return value != null ? Integer.valueOf(value) : defaultValue;
	}

	public Integer getIntegerProperty(String name)
	{
		return getIntegerProperty(name, null);
	}

	public Integer getIntegerProperty(String name, Integer defaultValue)
	{
		return getProperty(name, defaultValue);
	}
}
